﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using PrecedentLib;
using TfsComLib.TfsBranchLib2;

namespace TfsBranchLib2.Model
{
    public class BranchSetPrecedentContainer : PrecedentContainer<BranchSet>, IXmlSerializable
    {
        public BranchSetPrecedentContainer() : base(10, new TimeSpan(1, 0, 0), HashSet<BranchData>.CreateSetComparer())
        {
        }

        #region IXmlSerializable Members

        public XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(XmlReader reader)
        {
            var xmlSerializer = new XmlSerializer(typeof (PrecedentAggregator));
            reader.ReadStartElement("BranchSetPrecedentContainer");
            reader.ReadStartElement("Precedents");
            PrecedentAggregator precedentAggregator = null;
            do
            {
                precedentAggregator = (PrecedentAggregator) xmlSerializer.Deserialize(reader);
                if (precedentAggregator != null)
                {
                    Elements.Add(precedentAggregator);
                }
            } while (precedentAggregator != null);
            reader.ReadEndElement();
            reader.ReadEndElement();
        }

        public void WriteXml(XmlWriter writer)
        {
            var xmlSerializer = new XmlSerializer(typeof (PrecedentAggregator));
            writer.WriteStartElement("Precedents");
            foreach (var precedentAggregator in Elements)
            {
                xmlSerializer.Serialize(writer, precedentAggregator);
            }
            writer.WriteEndElement();
        }

        #endregion
    }
}